// $Id$
/*
 *  Copyright (C) 2007 Noury Solutions
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 3 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 */
/*! \file
 * \brief Definition of \c Salamanca.DataActivities.UI.WebControls.ControlQuestion.
 *
 * \author $Author$
 * \date $Date$
 * \version $Revision$
 */

using System;
using System.Web;
using System.Web.UI;

namespace Salamanca.DataActivities.UI.Web
{



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// <summary>Question implemented as a <see cref="Control" />.</summary>
    ///
    ///////////////////////////////////////////////////////////////////////////

    public class ControlQuestion:
        Question
    {

        /// <summary>Initializes a new <see cref="ControlQuestion" /> class.</summary>
        public ControlQuestion(string path, string controlId)
        {
            _ControlId=controlId;
            _Path=path;
        }

        /// <summary>Initializes a new <see cref="ControlQuestion" /> class.</summary>
        public ControlQuestion(Control control)
        {
            _Control=control;
            _ControlId=control.ID;
            _Path=HttpContext.Current.Request.Path;
        }

        /// <summary>Raises the <see cref="Question.Initialized" /> event.</summary>
        protected override void OnInitialized(QuestionEventArgs e)
        {
            if (HttpContext.Current.Server.MapPath(HttpContext.Current.Request.Path)!=HttpContext.Current.Server.MapPath(_Path))
                HttpContext.Current.Response.Redirect(_Path);

            if (_Control==null)
                _Control=FindControlRecursive((Control)HttpContext.Current.Handler, _ControlId);

            base.OnInitialized(e);

        }

        private static Control FindControlRecursive(Control root, string controlId)
        {
            if (root.ID==controlId)
                return root;

            Control ret=root.FindControl(controlId);
            if (ret!=null)
                return ret;

            foreach (Control c in root.Controls)
                if (c is INamingContainer)
                {
                    ret=FindControlRecursive(c, controlId);

                    if (ret!=null)
                        return ret;
                }

            return null;

        }

        /// <summary>Asks the question.</summary>
        public override ActivityStateResult Ask(IActivityController controller)
        {
            return ActivityStateResult.Stop;
        }

        /// <summary>.</summary>
        protected Control Control
        {
            get
            {
                return _Control;
            }
        }

        private Control _Control;
        private string _ControlId;
        private string _Path;
    }



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// <summary>Question implemented as a <see cref="Control" />.</summary>
    ///
    ///////////////////////////////////////////////////////////////////////////

    public class ControlQuestion<TAnswer>:
        ControlQuestion
    {

        /// <summary>Initializes a new <see cref="ControlQuestion{TAnswer}" /> class.</summary>
        public ControlQuestion(string path, string controlId):
            base(path, controlId)
        {
        }

        /// <summary>Initializes a new <see cref="ControlQuestion{TAnswer}" /> class.</summary>
        public ControlQuestion(Control control):
            base(control)
        {
        }

        /// <summary>Raises the <see cref="Question.Initialized" /> event.</summary>
        protected override void OnInitialized(QuestionEventArgs e)
        {
            base.OnInitialized(e);

            IDataBag<TAnswer> control=Control as IDataBag<TAnswer>;
            IDataBag<TAnswer> data=e.Controller.CurrentState.Data as IDataBag<TAnswer>;
            if ((control!=null) && (data!=null))
                control.Data=data.Data;
        }

        /// <summary>Raises the <see cref="Question.Answering" /> event.</summary>
        protected override void OnAnswering(AnswerEventArgs e)
        {
            base.OnAnswering(e);

            IAnswerContainer<TAnswer> control=Control as IAnswerContainer<TAnswer>;
            IDataBag<TAnswer> data=e.Data as IDataBag<TAnswer>;
            if (control!=null)
            {
                e.Answer=control.Answer;
                if (data!=null)
                    data.Data=control.Answer;
            }
        }
    }
}
